<?php

namespace app\common\model;

use app\admin\model\classes\Classes;
use think\Db;
use think\Model;

/**
 * 会员模型
 */
class UserBadge extends Model
{

    protected $pk = 'user_badge';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $append = [
        'level_text',
        'type_text',
        'createdtime_text',
    ];

    public function getTypeList()
    {
        return [
            1 => '全勤徽章',
            2 => '敏思徽章',
            3 => '佳绩徽章',
            4 => '燃萁徽章'
        ];
    }
    public function getTypeTextAttr($value, $data)
    {
        $typeList = $this->getTypeList();
        return isset($typeList[$data['type']]) ? $typeList[$data['type']] : '-';
    }

    public function getLevelList()
    {
        return [
            1 => '一星',
            2 => '二星',
            3 => '三星',
            4 => '四星',
            5 => '五星'
        ];
    }
    public function getLevelTextAttr($value, $data)
    {
        $levelList = $this->getLevelList();
        return isset($levelList[$data['level']]) ? $levelList[$data['level']] : '-';
    }

    public function getCreatedtimeTextAttr($value, $data)
    {
        return $data['createtime']?date('Y-m-d H:i:s', $data['createtime']):'-';
    }

    public function student()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

}
